package com.hentie.inputlib;

import android.view.MotionEvent;

public class TouchState {
	
	//Whether its multiTouch or not
	boolean isMultiTouch;
	//Is touching
	boolean isTouching;
	//How many fingers do we have
	int fingerCount, maxFingers;
	//Max fingers
	SingleTouch[] fingerTouch;
	

	
	
	public TouchState(boolean isMultiTouch, int maxFingers)
	{
		this.isMultiTouch = isMultiTouch;
		//The maximum amount of fingers
		this.maxFingers = maxFingers;
		//Initialis the touch interface
		fingerTouch = new SingleTouch[maxFingers];
		for(int i = 0; i < maxFingers; i++)
		{
			fingerTouch[i] = new SingleTouch();
		}
		
	}

	public void UpdateTouchState(MotionEvent ev,float viewWidth,float viewHeight)
	{

		isTouching = true;
		//get the number of fingers placed
		fingerCount = ev.getPointerCount();
		//Go through and setup the fingers
		for(int i = 0; i < maxFingers; i++)
		{
			if( i < fingerCount)
			{
				//Get the pointerID
				ev.getPointerId(i);

				fingerTouch[i].SetIsTouch(true);
				fingerTouch[i].UpdateTouchVector(ev.getX(i), ev.getY(i),viewWidth,viewHeight);
			}
			else
				fingerTouch[i].SetIsTouch(false);
		}
		
		
		
	}
	
	public SingleTouch GetFinger(int fingerID)
	{
		if(fingerID>maxFingers)
			return null;
		else
			return fingerTouch[fingerID];
		
	}
	
	public SingleTouch GetFirstFinger()
	{
		if(maxFingers >= 1)
			return fingerTouch[0];
		else return null;
	}
	
	public SingleTouch GetSecondFinger()
	{
		if(maxFingers >= 2)
			return fingerTouch[2];
		else return null;
	}	
	
	//Call when touching has stopped
	public void NoTouch()
	{
		isTouching = false;
		fingerCount = 0;
		for(int i = 0; i < maxFingers; i++)
		{
			fingerTouch[i].SetIsTouch(false);
		}
		
		
	}
	
	public boolean IsTouching()
	{
		return isTouching;
	}
	
}
